<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="年龄" prop="age">
      <el-input v-model="form.age" placeholder="请输入年龄" />
    </el-form-item>
    <el-form-item label="身高/cm" prop="height">
      <el-input v-model="form.height" placeholder="请输入身高/cm" />
    </el-form-item>
    <el-form-item label="体重/kg" prop="weight">
      <el-input v-model="form.weight" placeholder="请输入体重/kg" />
    </el-form-item>
    <el-form-item label="目标步数" prop="targetStep">
      <el-input v-model="form.targetStep" placeholder="请输入目标步数" />
    </el-form-item>
    <el-form-item label="目标体重/kg" prop="targetWeight">
      <el-input v-model="form.targetWeight" placeholder="请输入目标体重/kg" />
    </el-form-item>
    <el-form-item label="身体质量指数BMI" prop="BMI">
      <el-input v-model="form.bmi" placeholder="保存基本信息以后系统生成" disabled />
    </el-form-item>
    
    <el-button type="primary" size="mini" @click="submit">保存</el-button>
    <el-button type="danger" size="mini" @click="close">关闭</el-button>
  </el-form>
</template>

<script>
import { getSelf, addSelf } from "@/api/system/self";
import { create } from "sortablejs";

export default {
  data() {
    return {
      form: {
        age: '',
        height: '',
        weight: '',
        targetStep: '',
        targetWeight: '',
        bmi: '',
        medicalHistory: '',
        isSmoke: '',
        allergiesHistory: '',
      },
      // 表单校验
      rules: {
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" }
        ],
        height: [
          { required: true, message: "身高不能为空", trigger: "blur" }
        ],
        weight: [
          { required: true, message: "身高不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      getSelf().then(res => {
        // 容错处理，防止为 null 或 undefined
        const data = res.data || {};
        this.form.age = data.age !== undefined && data.age !== null ? data.age : '';
        this.form.height = data.height !== undefined && data.height !== null ? data.height : '';
        this.form.weight = data.weight !== undefined && data.weight !== null ? data.weight : '';
        this.form.targetStep = data.targetStep !== undefined && data.targetStep !== null ? data.targetStep : '';
        this.form.targetWeight = data.targetWeight !== undefined && data.targetWeight !== null ? data.targetWeight : '';
        this.form.bmi = (data.bmi !== undefined && data.bmi !== null && data.bmi !== '') ? parseFloat(data.bmi).toFixed(2) : '';
        this.form.medicalHistory = data.medicalHistory !== undefined && data.medicalHistory !== null ? data.medicalHistory : '';
        this.form.isSmoke = data.isSmoke !== undefined && data.isSmoke !== null ? data.isSmoke : '';
        this.form.allergiesHistory = data.allergiesHistory !== undefined && data.allergiesHistory !== null ? data.allergiesHistory : '';
      })
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addSelf(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
